﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using ESF.General;
using ESF.General.Entity;
using ESF.WinForms;
using ESF.WinForms.Controls.Barcodes;
using MessagingToolkit.Barcode;

#endregion


namespace ESF.FileHelper.Controls
{
	public partial class uscGenerateBarcodeStickers : UserControl
	{
		#region Members

		private QRStickerPrintPage _QRStickerPrintPage;

		#endregion


		#region Constructor
		
		public uscGenerateBarcodeStickers()
		{
			InitializeComponent();

			ListLoader.LoadList (ddlIncludeBarCode, "Código Completo", "All", "Código Separado", "Separated");

			ddlIncludeBarCode.SelectedIndex = 2;

			ListLoader.LoadList (ddl2DType, 
				new List<BasicReference> ()
				{
					new BasicReference () { Code = BarcodeFormat.QRCode.ToString (), Name = "QR" },
					new BasicReference () { Code = BarcodeFormat.DataMatrix.ToString (), Name = "DataMatrix" }//,
					//new BasicReference () { Code = BarcodeFormat.Aztec.ToString (), Name = "Aztec" }
				});

			ListLoader.RemoveSelectItem (ddl2DType);
		}

		#endregion


		#region Methods

		private bool GenerateStickerDocument ()
		{
			StickerPageSettings pagSettings;

			List<Font> stickerFonts;

			List<StickerData> stickerData;

			int particularStart, particularEnd;
			
			
			int.TryParse (txtBarcodeParticularStart.Text, out particularStart);
			

			particularEnd = (int) (particularStart + nupStickerCount.Value);
		
			if (TextHelper.IsEmpty (txtBarcodeGeneral.Text))
			{
				MessageBox.Show (this, "Diligencie la parte general");
				return false;
			}

			if (TextHelper.IsEmpty (txtTitle.Text))
			{
				MessageBox.Show (this, "Diligencie el titulo");
				return false;
			}

			pagSettings = new StickerPageSettings ()
			{
				StickerHeight_mm = (int) nupStickerHeight.Value,
				StickerWidth_mm = (int) nupStickerWidth.Value,
				StickerHorizontalSeparator_mm = (int) nupHorizontalSeparator.Value,
				StickerVerticalSeparator_mm = (int) nupVerticalSeparator.Value,
				StickerRowCount = (int) nupStickerRowCount.Value,
				StickerPageCount = (int) nupStickerPageCount.Value,
				PageMarginTop_mm = (int) nupPageMarginTop.Value,
				PageMarginRight_mm = (int) nupPageMarginRight.Value,
				PageMarginBottom_mm = (int) nupPageMarginBottom.Value,
				PageMarginLeft_mm = (int) nupPageMarginLeft.Value,
				StickerIncludeBorders = chkIncludeBorder.Checked,
				BarcodeWidth = (int) nup2DWidth.Value,
				BarcodeHeight = (int) nup2DHeight.Value,
				BarcodeFormat = (BarcodeFormat) Enum.Parse (typeof (BarcodeFormat), (ddl2DType.SelectedItem as BasicReference).Code),
				BarcodeModuleSize = (int) nupModuleSize.Value
			};

			stickerFonts = new List<Font> ()
			{
				new Font ("Arial", 9),
				new Font ("Arial", 8)
			};

			stickerData = new List<StickerData> ();

			for (; particularStart < particularEnd; particularStart++)
			{
				stickerData.Add (
					new StickerData ()
					{
						General = txtBarcodeGeneral.Text,
						Particular = string.Format ("{0," + txtBarcodeParticularStart.Mask.Length + ":" + new String ('0', txtBarcodeParticularStart.Mask.Length) + "}", particularStart),
						TextFonts = stickerFonts,
						Texts = new List<string> () { txtTitle.Text }
					});
			}

			_QRStickerPrintPage = new QRStickerPrintPage (new System.Drawing.Printing.PrinterSettings (), pagSettings, stickerData, stickerFonts);
			

			if (ddlIncludeBarCode.SelectedItem != null)
			{
				_QRStickerPrintPage.ShowBarcodeSeparated = (ddlIncludeBarCode.SelectedItem as ESF.General.Entity.BasicReference).Code == "Separated";
				_QRStickerPrintPage.ShowBarcode = (ddlIncludeBarCode.SelectedItem as ESF.General.Entity.BasicReference).Code == "All";
			}

			ppcDocumentPreview.SetDocument (_QRStickerPrintPage, _QRStickerPrintPage.PageCount);
			
			return true;
		}

		#endregion


		#region Button Events

		private void btnGenerateSticker_Click(object sender, EventArgs e)
		{
			ppcDocumentPreview.Visible = btnPrint.Enabled = GenerateStickerDocument ();
		}


		private void btnPrint_Click(object sender, EventArgs e)
		{
			if (chkUseDefaultPrinter.Checked)
			{
				_QRStickerPrintPage.Print ();
			}
			else if (prdPrintDialog.ShowDialog (this) == DialogResult.OK)
			{
				_QRStickerPrintPage.PrinterSettings = prdPrintDialog.PrinterSettings;				
				_QRStickerPrintPage.Print ();
			}
		}

		#endregion


		#region Numeric Events

		private void nupStickerRowCount_ValueChanged(object sender, EventArgs e)
		{
			nupStickerPageCount.Increment = nupStickerRowCount.Value;
			nupStickerCount.Increment = nupStickerRowCount.Value;
		}

		#endregion
	}
}
